/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config.javabeans;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.exception.ConfigurationException;
import opiam.admin.faare.exception.PersistenceException;

import netscape.ldap.LDAPv2;

import org.apache.log4j.Logger;

import java.io.Serializable;

import java.util.Enumeration;
import java.util.Properties;


/**
 * Class corresponding to the "ressource" element of the profiles_config.xml file.<br>
 * This is the definition of a resource.
 */
public class JBRessource implements Cloneable, Serializable
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(JBRessource.class);

    /** LDAP scope parameter. */
    public static final String PARAM_LDAP_SCOPE = "ldap-scope";

    /** LDAP base parameter. */
    public static final String PARAM_LDAP_BASE = "ldap-base";

    /** LDAP filter parameter. */
    public static final String PARAM_LDAP_FILTER = "ldap-filter";

    /** Object filter parameter. */
    public static final String PARAM_OBJECT_FILTER = "object-filter";

    /** Login attribute parameter. */
    public static final String PARAM_LOGIN_ATTRIBUTE = "login-attribute";

    /** Resource name. */
    private String name;

    /** Business object: ex: myapp.javabeans.JBInetOrgPerson. */
    private String type;

    /** Resource specific parameters. */
    private Properties param = new Properties();

    /** Associated class descriptor. */
    private JBClassDescriptor classDesc;

    /** Creates new JBRessource. */
    public JBRessource()
    {
    }

    /**
     * Returns the resource name.
     * @return name
     */
    public String getName()
    {
        return name;
    }

    /**
     * Returns the parameter from properties with key = PARAM_LDAP_SCOPE.
     *
     * @return parameter value
     */
    public int getLdapScopeParam()
    {
        String scopeStr = param.getProperty(PARAM_LDAP_SCOPE);

        if (scopeStr == null)
        {
            return LDAPv2.SCOPE_SUB;
        }
        else
        {
            return Integer.parseInt(scopeStr);
        }
    }

    /**
     * Returns the parameter from properties with key = PARAM_LDAP_BASE.
     *
     * @return parameter value
     */
    public String getLdapBaseParam()
    {
        String baseSearch = param.getProperty(PARAM_LDAP_BASE);

        return baseSearch;
    }

    /**
     * Returns the parameter from properties with key = PARAM_LDAP_FILTER.
     *
     * @return parameter value
     */
    public String getLdapFilterParam()
    {
        return param.getProperty(PARAM_LDAP_FILTER);
    }

    /**
     * Returns the parameter from properties with key = PARAM_OBJECT_FILTER.
     *
     * @return parameter value
     */
    public String getObjectFilterParam()
    {
        return param.getProperty(PARAM_OBJECT_FILTER);
    }

    /**
     * Returns the parameter from properties with key = PARAM_LOGIN_ATTRIBUTE.
     *
     * @return parameter value
     */
    public String getLoginAttributeParam()
    {
        return param.getProperty(PARAM_LOGIN_ATTRIBUTE);
    }

    /**
     * Returns the resource parameters.
     * @return parameters
     */
    public Properties getParam()
    {
        return param;
    }

    /**
     * Returns the associated business object name.
     * @return class name
     */
    public String getType()
    {
        return type;
    }

    /**
     * Sets the resource name.
     * @param aname The name to set
     */
    public void setName(String aname)
    {
        this.name = aname;
    }

    /**
     * Sets the associated business object name.
     * @param atype The class name
     */
    public void setType(String atype)
    {
        this.type = atype;
        _logger.debug("setType type : " + type);

        try
        {
            this.classDesc = PropertiesManager.getInstance()
                                              .getPersistenceDescriptionMap()
                                              .getClassDescriptor(type);
            _logger.debug("setType classDesc : " + classDesc.getName());
        }
        catch (PersistenceException e)
        {
            e.printStackTrace();
            throw new ConfigurationException("setType error " + type);
        }

        if (classDesc == null)
        {
            throw new ConfigurationException("setType classDesc == null");
        }
    }

    /**
     * Adds a resource parameter.
     *
     * @param key parameter type
     * @param value parameter value
     */
    public void addParam(String key, String value)
    {
        param.setProperty(key, value);
    }

    /**
     * Displays the resource definition.
     *
     * @return resource definition
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;

        buf.append("name = ");
        buf.append(name);
        buf.append(System.getProperty("line.separator"));
        buf.append("type = ");
        buf.append(type);
        buf.append(System.getProperty("line.separator"));

        if (param != null)
        {
            buf.append("Properties components of param = ");
            buf.append(System.getProperty("line.separator"));

            Enumeration enum1 = param.propertyNames();
            String key;

            while (enum1.hasMoreElements())
            {
                buf.append("...");
                key = (String) enum1.nextElement();
                buf.append(key);
                buf.append(" = ");
                buf.append(param.getProperty(key));
                buf.append(System.getProperty("line.separator"));
            }
        }

        return buf.toString();
    }

    // end of toString method

    /**
     * Returns the associated class descriptor.
     * @return class descriptor
     */
    public JBClassDescriptor getClassDesc()
    {
        return classDesc;
    }

    /**
     * see Object.clone().
     *
     * @return see Object.clone()
     */
    @Override
	public Object clone()
    {
        Object o = null;

        try
        {
            o = super.clone();
            ((JBRessource) o).param = (Properties) this.param.clone();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return o;
    }
}
